<?php

class ArchiveModel extends Model
{
	public function getinfo($id,$type='id')
	{
		if ($type=='key'){
			$where=array('key' => $id);
		}else{
			$where=array('id' => $id);
		}
		$archiveinfo = $this->where($where)->find();
        if ($archiveinfo){
            $module=$archiveinfo['module'];
            $addoninfo = M($module)->parseinfo(M($module)->where(array('archive_id' => $archiveinfo['id']))->find());
            if ($addoninfo && $archiveinfo) {
                $archiveinfo['category_name']=dc::get('category',$archiveinfo['category_id'],'name');
                $archiveinfo['category_key']=dc::get('category',$archiveinfo['category_id'],'key');
                $archiveinfo['category_url']=M('category')->geturl($archiveinfo['category_id']);
                return array_merge($archiveinfo,$addoninfo);
            }
        }
		return false;
	}

	public function remove($module)
	{
		$where['archive_id'] = $this->parts['where']['0']['id'];
		M($module)->where($where)->delete();
		parent::delete();
	}

	public function getlist($field = '')
	{
		if ($field === '') {
			$field = 'title,category_id,key,module,updatetime';
		} else {
			$arr = explode(',', trim($field, ','));
			$field = implode(',', array_merge($arr, array_diff(array('id','category_id', 'key', 'module'),$arr)));
		}
		$list = (array)$this->field($field)->select();
		foreach ($list as &$v) {
			$v['category_name'] = dc::get('category',$v['category_id'], 'name');
			$v['url_category'] = M('category')->geturl($v['category_id']);
            $v['url_archive'] = $this->geturl($v['module'], $v['key'], $v['category_id']);
		}
		return $list;
	}

	public function geturl($module, $key, $category_id)
	{
        if ($module=='bbs'){
            return U('bbs.thread.index',array('id'=>$key));
        }
		$category_info = dc::get('category',$category_id);
		if ($category_info['level'] == '1') {
			$dir = $category_info['key'];
		} else {
            $paths=explode(',',$category_info['path']);
			$dir = M('category')->where("id={$paths['2']}")->getfield('key');
		}
		return U('index.' . $module . '.detail', array('dir' => $dir, 'key' => $key));
	}

	public function getPre($id,$cid)
	{
		$info=$this->field('key,title,module,category_id')->where('status=1 and category_id='.$cid.' and id<'.$id)->order('id desc')->limit(1)->find();
		if (empty($info['key'])){
			return array('url'=>'#','title'=>'没有了');
		}else{
			return array('url'=>$this->geturl($info['module'],$info['key'],$info['category_id']),'title'=>$info['title']);
		}
	}

	public function getNext($id,$cid)
	{
		$info=$this->field('key,title,module,category_id')->where('status=1 and category_id='.$cid.' and id>'.$id)->order('id asc')->limit(1)->find();
		if (empty($info['key'])){
			return array('url'=>'#','title'=>'没有了');
		}else{
			return array('url'=>$this->geturl($info['module'],$info['key'],$info['category_id']),'title'=>$info['title']);
		}
	}

    public function uploadcontent($id,$firstimg)
    {
        preg_match_all('{<img[^>]+?src=[\'"]{0,1}(.+?)[\'">\s]}i',$_POST['content'],$match);
        $upload = new upload();
        C('storage_path', 'uploads');
        foreach($match['1'] as $imgurl){
            //网络图片 需要上传
            if (stripos($imgurl,'http')===0){
                $content=http::get($imgurl);
                $hash=md5($content);
                $url = M('attachment')->where(array('hash' => $hash))->getfield('url');
                if (!$url){
                    $uploadres=$upload->uploadurl($imgurl,$content);
                    if ($uploadres['status']){
                        $url=$uploadres['info']['fileurl'];
                        $image = new image(url2path($uploadres['info']['fileurl']));
                        $image->water(C('water_file'));
                        storage::write($uploadres['info']['filepath'],$image->save());
                        $param = array(
                            'user_id' => intval(cookie('userid')),
                            'name' => $uploadres['info']['filename'],
                            'path' => $uploadres['info']['filepath'],
                            'url' => $uploadres['info']['fileurl'],
                            'ext' => $uploadres['info']['ext'],
                            'size' => $uploadres['info']['size'],
                            'hash' => $uploadres['info']['hash'],
                            'status'=>1,
                            'archive_id'=>$id,
                        );
                        M('attachment')->add($param);
                    }
                }
            }else{
                $url=$imgurl;
            }
            //首张图替换
            if (!$firstimg && $url){
                $image = new image(url2path($url));
                $image->thumb(C('thumb_width'),C('thumb_height'));
                $file=date('Ym') . '/' . date('d').'/thumb_'.basename($url);
                storage::write($file,$image->save());
                $_POST['litpic']=$firstimg=storage::geturl($file);
                $this->where(array('id'=>$id))->update(array('litpic'=>$firstimg));
            }
            $_POST['content']=str_replace($imgurl,$url,$_POST['content']);
        }
    }
}